package com.xneure.code.excel;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFCellUtil;
import org.apache.poi.hssf.util.HSSFColor.GREY_25_PERCENT;
import org.apache.poi.ss.util.CellRangeAddress;

import com.xneure.code.upload.Handling;
import com.xneure.utils.exception.ParseException;


public abstract class ExcelExport implements ExcelExportHandler{
	protected HSSFWorkbook workbook = new HSSFWorkbook();
	protected HSSFCellStyle cellDataStyle;
	private Map<String,String> param;
	protected static final short fontSize = 10;
	protected static short rowHigth = 60;

	public ExcelExport(){
		cellDataStyle = getCellStyle();
	}

	public void initParam(Map<String,String> par){
		this.param = par;
	}

	protected short getRowHigth(int defHeight,String data,int strCount){
		return (short)(defHeight * stringRowCount(data,strCount));
	}

	public Handling getHandling(){
		return Handling.ROLLBACK;
	}

	protected void setValue(HSSFCell cell,Column c){
		String tem = c.getData();
		if(c.getType() == HSSFCell.CELL_TYPE_STRING){
			cell.setCellValue(isNumeric(c.getData()) ? String.valueOf(formatValue(c.getData())) : c.getData());
		}else{
			double f1 = formatValue(tem);
			cell.setCellValue(f1);
		}
	}

	protected boolean isNumeric(String str){
		if(null == str || str.trim().length() < 1){
			return false;
		}
		try{
			Double.parseDouble(str);
			return true;
		}catch(NumberFormatException e){
			try{
				Long.parseLong(str);
				return true;
			}catch(NumberFormatException e1){
				return false;
			}
		}
	}

	protected double formatValue(String tem){
		BigDecimal bg = new BigDecimal(tem);
		double f1 = bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	protected String getParam(String name){
		if(param.containsKey(name) == false){
			throw new ParseException("参数：" + name + " 未指定。");
		}
		return param.get(name);
	}

	/**
	 * 计算字符串的行数,忽略换行符。
	 * 
	 * @param data 要计算的数据
	 * @param rowCount 每行显示的字符串
	 * @return int
	 */
	protected int stringRowCount(String data,int rowCount){
		if(null == data || data.length() < 1){
			return 0;
		}
		char[] tb = data.toCharArray();
		if(tb.length <= rowCount){
			return 1;
		}
		int l = 0, len = 1;
		boolean en = false;
		for(int i = 0;i < tb.length;i++){
			if(tb[i] == 13 || tb[i] == 10){
				continue;
			}
			if(((tb[i] & 0xFF00) >> 8) == 0){
				if(en == false){
					++l;
				}else{
					--l;
				}
				en = !en;
			}else{
				l++;
			}
			if(l == rowCount){
				len++;
				l = 0;
			}
		}
		return l < 0 ? len - 1 : len;
	}

	protected void mergeCell(HSSFSheet sheet,String excelAddress){
		CellRangeAddress valueOf = CellRangeAddress.valueOf(excelAddress);
		sheet.addMergedRegion(valueOf);
	}

	protected void mergeCell(HSSFSheet sheet,int firstRow,int lastRow,int firstCol,int lastCol){
		CellRangeAddress valueOf = new CellRangeAddress(firstRow,lastRow,firstCol,lastCol);
		sheet.addMergedRegion(valueOf);
	}

	/**
	 * 设置合并单元格格式
	 * 
	 * @param sheet
	 * @param region
	 * @param cs
	 */
	protected void setRegionStyle(HSSFSheet sheet,CellRangeAddress region,HSSFCellStyle cs){
		for(int i = region.getFirstRow();i <= region.getLastRow();i++){
			HSSFRow row = HSSFCellUtil.getRow(i,sheet);
			for(int j = region.getFirstColumn();j <= region.getLastColumn();j++){
				HSSFCell cell = HSSFCellUtil.getCell(row,(short)j);
				cell.setCellStyle(cs);
			}
		}
	}

	protected HSSFCellStyle getTitleCellStyle(){
		HSSFCellStyle s = getCellStyle();
		s.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		s.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		s.setFillBackgroundColor(new GREY_25_PERCENT().getIndex());
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(fontSize);
		font.setFontName("宋体");
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);         // 字体增粗
		s.setFont(font);
		return s;
	}

	public HSSFCellStyle getCellStyle(){
		HSSFCellStyle s = workbook.createCellStyle();
		s.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
		s.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
		s.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
		s.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
		s.setWrapText(true);
		return s;
	}
}
